package com.roy.netty.pojoTransfer;

import io.netty.channel.*;

/**
 * @author ：楼兰
 * @date ：Created in 2021/6/8
 * @description:
 **/

public class PojoNettyServerHandler extends SimpleChannelInboundHandler<User> {


    //接收到的消息会经过PojoDecoder转码成user对象，进来处理。
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, User user) throws Exception {
        user.addSal();
        final ChannelFuture future = ctx.writeAndFlush(user);
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if(future.isDone()){
                    System.out.println("给用户 "+user.getUserId()+" 添加薪水后返回给客户端");
                }
            }
        });
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("发生异常 "+cause.getMessage());
        ctx.close();
    }
}
